package com.xjggb.cloud.controller;

import com.xjggb.cloud.entity.CommonResult;
import com.xjggb.cloud.entity.Payment;
import com.xjggb.cloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private int port;

    @Autowired
    private PaymentService paymentService;


    @PostMapping("/payment/add")
    public CommonResult add(@RequestBody Payment payment){
        System.out.println("payment = " + payment.getSerial());

        int add = paymentService.add(payment);
        log.info("插入数据的ID:\t" +payment.getId());
        log.info("插入的结果：\t"+payment);
        if (add>0){
            return new CommonResult(200,"插入成功 ",add);
        }else {
            return new CommonResult(444,"插入数据失败",null);
        }

    }


    @GetMapping("/payment/get/{id}")
    public CommonResult agePaymentById(@PathVariable("id") Long id){


        Payment payment = paymentService.getPayment(id);
        log.info("查询结果"+payment);
        if (payment!=null){
            return new CommonResult(200,"查询成功 端口号为"+port,payment);

        }else {

            return new CommonResult(444,"没有对应记录",null);
        }

    }

    @GetMapping("/payment/lb")
    public int getPaymentLB() {
        return port;
    }



}
